<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 搜索区域 -->
      <!-- 操作区域 -->
      <page-tools>
        <template #after>
          <el-button size="small" type="primary" @click="addPerm(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格区域 -->
      <el-table
        row-key="id"
        :data="list"
        border
        stripe
      >
        <el-table-column
          v-for="(col,index) in columns"
          :key="index"
          :prop="col.prop"
          :label="col.label"
          :align="col.align"
        >
          <template slot-scope="{row}">
            <el-tag v-if="col.prop==='code'">
              {{ row[col.prop] }}
            </el-tag>
            <span v-else>{{ row[col.prop] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{row}">
            <el-button type="text" @click="addPerm(2,row.id)">添加</el-button>
            <el-button type="text" @click="editRow(row.id)">编辑</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
    </div>

    <!-- 新增和修改弹窗 -->
    <addForm ref="addForm" @updateList="initData" />
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
import addForm from './component/addForm.vue'

export default {
  name: 'Permission',
  components: {
    addForm
  },
  data() {
    return {
      // 表格数据
      list: [],
      // 列的数据
      columns: [
        { label: '名称', prop: 'name', align: 'center' },
        { label: '标识', prop: 'code', align: 'center' },
        { label: '描述', prop: 'description', align: 'center' }
      ]
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取表格的数据
    async initData() {
      const res = await getPermissionList()
      // console.log(res)
      // this.list = res
      // 树形结构
      this.list = tranListToTreeData(res, '0')
    },
    // 添加权限
    addPerm(type, pid) {
      this.$refs.addForm.formData.type = type
      this.$refs.addForm.formData.pid = pid
      this.$refs.addForm.dialogShow = true
    }
  }
}
</script>

<style>

</style>
